//
//  ParameterItemView.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/21/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

protocol ParameterItemProtocol: class {
    var container: UIView { get set }
    var iconBtn: UIButton { get set }
    var titleLabel: UILabel { get set }
    func setUpView (value: String?, icon: UIImage?, placeholder: String)
}

protocol ParameterItemDelegate {
    func didRemoveValue (value: String?, placeholder: String, itemIndex: Int)
}

final public class ParameterItem: ParameterItemProtocol {
    
    // MARK: - UI Views
    
    lazy var container: UIView = {
        var view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    lazy var iconBtn: UIButton = {
       var img = UIButton()
       img.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
       }
       return img
    }()
    
    lazy var labelsStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.alignment = .leading
        stack.spacing = 4
        stack.distribution = .equalCentering
        return stack
    }()
    
    lazy var titleLabel: UILabel = {
        var lbl = UILabel()
        lbl.textColor = placeholderColor
        lbl.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        return lbl
    }()
    
    lazy var placeholderLabel: UILabel = {
       let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 10.0, weight: .regular)
        lbl.textColor = .gray
        return lbl
    }()
    
    
    // MARK: - Props
    
    private weak var parentView: UIStackView!
    private var viewIndex: Int = 0
    private let placeholderColor = UIColor.greyColor
    
    private var _value: String?
    private var _icon: UIImage?
    private var _placeholder: String!
    
    var itemIndex: Int!
    var delegate: ParameterItemDelegate!
    
    // MARK: - Initialization
    
    required init (view: UIStackView) {
        self.parentView = view
        view.addArrangedSubview(self.container)
        addSubviews()
        addConstraints()
    }

    @objc func iconClick (sender: UIButton) {
        if (_value != nil) {
            delegate.didRemoveValue(value: _value,
                                    placeholder: _placeholder,
                                    itemIndex: itemIndex)
            self.setUpView(value: nil, icon: _icon, placeholder: _placeholder)
        }
    }
    
    public func setUpView (value: String?=nil, icon: UIImage?=nil, placeholder: String) {
        self._value = value
        self._icon = icon
        self._placeholder = placeholder
        if (value != nil && value != "") {
            if !(labelsStack.arrangedSubviews.contains(placeholderLabel)) {
                labelsStack.addArrangedSubview(placeholderLabel)
            }
            placeholderLabel.text = placeholder
            titleLabel.text = value
            titleLabel.textColor = .black
            iconBtn.setImage(UIImage(named: "close"), for: .normal)
        } else {
            placeholderLabel.text = ""
            if (labelsStack.arrangedSubviews.contains(placeholderLabel)) {
                labelsStack.removeArrangedSubview(placeholderLabel)
            }
            titleLabel.text = placeholder
            titleLabel.textColor = placeholderColor
            iconBtn.setImage(icon, for: .normal)
        }
    }
    
    private func addSubviews() {
        container.addSubview(labelsStack)
        container.addSubview(iconBtn)
        labelsStack.addArrangedSubview(placeholderLabel)
        labelsStack.addArrangedSubview(titleLabel)
        iconBtn.addTarget(self, action: #selector(iconClick(sender:)), for: .touchUpInside)
    }
    
    // MARK:- Constraints
   
    private func addConstraints () {
        container.snp.makeConstraints { (make) in
            make.height.equalTo(48)
            make.leading.trailing.equalToSuperview()
        }
        
        labelsStack.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.trailing.equalTo(iconBtn.snp.leading).offset(-8)
        }
        
        iconBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(15)
        }
        
        placeholderLabel.snp.makeConstraints { (make) in
            make.height.equalTo(10)
        }
    }
}

